package com.xxd.utils;

import com.auth0.jwt.JWT
import com.auth0.jwt.JWTVerifier
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT
import org.apache.shiro.crypto.SecureRandomNumberGenerator;

import java.util.Date;

/**
 * @Author XuXd
 * @Description: token 工具类
 * @CreateDate: 11:19 2019-09-17
 * @Modified by: *
 */
class JwtUtil {

    static final USER_CODE = "user_code";
    static final USER_NAME = "user_name"
    //默认30分钟失效
    static final DEFAULT_TIME = 1800
    /**
     * 获取签发时间
     * @param token
     * @return
     */
    static Date getIssuedAt(String token){
        try {
            DecodedJWT jwt = JWT.decode(token);
            return jwt.getIssuedAt();
        }catch (Exception e){
            return null;
        }
    }

    /**
     * 获取用户名ID
     * @param token
     * @return
     */
    static String getUserName(String token){
        try {
            DecodedJWT jwt = JWT.decode(token);
            return jwt.getClaim(this.USER_NAME).asString();
        }catch (Exception e){
            return null;
        }
    }

    /**
     * 签名 token
     * @param username
     * @param salt
     * @param time 有效时长 单位：秒 second
     * @return
     */
    static String sign(String username,String salt,long time){
        try {
            //-1表示永不过期，默认100年有效期
            long newTime = (time >=0?time : 365*50*24*60*60 ) * 1000
            Date date = new Date(System.currentTimeMillis()+newTime);
            //println( "${new Date()} ${date}  ::::${newTime}")
            Algorithm algorithm = Algorithm.HMAC256(salt);
            // 附带username信息
            return JWT.create()
                    .withClaim(USER_CODE, username)
                    .withExpiresAt(date)
                    .withIssuedAt(new Date())
                    .sign(algorithm);
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }

    static String sign(String username,String salt) {
        return sign(  username,  salt,  this.DEFAULT_TIME)
    }

    /**
     * token是否过期
     * @return true：过期
     */
    static boolean isExpired(String token) {
        Date now = Calendar.getInstance().getTime();
        DecodedJWT jwt = JWT.decode(token);
        return jwt.getExpiresAt().before(now);
    }

    /**
     * 校验token
     * @param token
     * @return
     */
    static boolean verify(String token,String user,String salt){
        try {
            Algorithm algorithm = Algorithm.HMAC256(salt);
            JWTVerifier verifier = JWT.require(algorithm)
                    .withClaim(USER_CODE, user)
                    .build();
            verifier.verify(token);
            return true
        }catch(Exception e){
            e.printStackTrace()
            return false
        }
    }

    static void main(String[] args) {
        System.out.println(JwtUtil.sign("xxd","xxd" ,-1))
        System.out.println(JwtUtil.verify("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2NvZGUiOiJ4eGQiLCJleHAiOjE1Njg3ODU3ODYsImlhdCI6MTU2ODc4Mzk4Nn0.wtOUQggChRZYdBazJ9X65IZlAkDqY-8ksrjyAkCgK1E",
                "xxd","e27c396e129413fa79aacfa26391f966" ))

    }

}
